package com.xubeihong.xubeihong;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.xubeihong.xubeihong.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;

@Slf4j
public class HttpTool {
    // 改id  改签名
    private  static  String url = "https://muspace.smartechnology.com.cn/api/new_active_order";
    //14591
    private static String entityStr = "{\"p\":\"w\",\"Sign\":\"RootSign\",\"active_time_id\":\""+Constant.id+"\",\"contact_phone\":\"1569504\",\"alternate\":\"\",\"toi_cardtype_id\":1,\"spot\":0,\"person\":[{\"toi_cardtype_id\":1,\"toi_username\":\"郝柳\",\"toi_card_num\":\"1304811096\"}],\"notes\":\"\",\"children\":[],\"toi_unit\":\"\",\"uid\":\"P%3AhWT596H%2B3%2BICj%2BRVlOzsdA%3D%3D\",\"_sign\":[\"H/+a0jA9a1x/GOpinq6EPtEc0C/HUjUUkowh8o9M84PBeTOYzLf7Lpt9IRo2WmfVqbIqykjzAlcS+E8Wv4rFAuTlQH3w/hSs/pq4v45j1cujl5dOnk9RHyPYVQDhmGVKV7cE2pG1edByI+g7wBao9ypRy4Elf+ml8EQTPl+zc2TXlzpNscCIDbbe4YMztRLhfOga40yk1Yf/UUQfMd1TXzWsHrSx9NPKJoVwXNtadw6NmqvTkcIk0vim9VpShaqiRqhn9tU+fvsv4YO39AP9hp7+CN/BMqPpAEzxjzyfybanYz0AdNAjpBt0TYcyW9LDELppQ7rggSrhQ+4QV80Qsg==\"],\"api_token\":\"efd69a911cb0df16522d0c9349273914\"}";

    public static JSONObject post()  {

        CloseableHttpResponse response ;
        JSONObject result=null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(50000) // 连接超时时间，单位毫秒
                .setSocketTimeout(50000)  // 读取超时时间，单位毫秒
                .build();
        post.setConfig(requestConfig);
        post.setHeader("Sec-Fetch-Dest","empty");
        post.setHeader("Sec-Fetch-Site","cross-site");
        post.setHeader("Host","muspace.smartechnology.com.cn");
        post.setHeader("Accept","application/json");
        post.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11275");
        //post.setHeader("Content-Length","712");
        post.setHeader("xweb_xhr","1");
        post.setHeader("Accept-Language","zh-CN,zh;q=0.9");
        post.setHeader("Accept-Encoding","gzip, deflate, br");
        post.setHeader("Sec-Fetch-Mode","cors");
        post.setHeader("Content-Type","application/json");
        post.setHeader("Referer","https://servicewechat.com/wx607b92668342d8ca/144/page-frame.html");
        post.setHeader("Connection","keep-alive");

        post.setEntity(new StringEntity(entityStr, ContentType.APPLICATION_JSON));


        try{
            response = httpclient.execute(post);
            String body = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            result = JSONObject.parseObject(body);
            //System.out.println(result);
            log.info("抢票结果"+result.toString());

        }catch (Exception e){
            log.error("请求接口出错"+e.toString());
            //e.printStackTrace();
            JSONObject res = new JSONObject();
            res.put("status",-1);
            return res;
        }


        return result;
    }
}
